package com.rayo.accesscontrollercopy.module.setting;

import android.content.DialogInterface;
import android.support.v7.app.AlertDialog;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.Spinner;

import com.rayo.accesscontrollercopy.R;
import com.rayo.accesscontrollercopy.app.BaseFragment;
import com.rayo.accesscontrollercopy.model.db.bean.ReaderData;
import com.rayo.accesscontrollercopy.module.setting.presenter.ReaderPresenter;
import com.rayo.accesscontrollercopy.module.setting.view.IReaderView;


/**
 * Created by yanghaojie on 16/7/21.
 */
public class ReaderFragment extends BaseFragment implements IReaderView, AdapterView.OnItemSelectedListener {
    private Spinner mSpPlugInConnectBeep, mSpPlugInUpdateBeep, mSpPlugInEventBeep,
            mSpInsetConnectBeep, mSpInsetUpdateBeep, mSpInsetEventBeep;
    private Button mBtnPlugInTestKey, mBtnPlugInTestCard, mBtnInsetTestKey, mBtnInsetTestCard;

    private ReaderPresenter presenter;
    private ArrayAdapter<String> mBeepArray;

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_reader;
    }

    @Override
    protected void setupViews() {
        mSpPlugInConnectBeep = (Spinner) rootView.findViewById(R.id.spinner_plug_in_connect_beep);
        mSpPlugInUpdateBeep = (Spinner) rootView.findViewById(R.id.spinner_plug_in_update_beep);
        mSpPlugInEventBeep = (Spinner) rootView.findViewById(R.id.spinner_plug_in_event_beep);
        mSpInsetConnectBeep = (Spinner) rootView.findViewById(R.id.spinner_inset_connect_beep);
        mSpInsetUpdateBeep = (Spinner) rootView.findViewById(R.id.spinner_inset_update_beep);
        mSpInsetEventBeep = (Spinner) rootView.findViewById(R.id.spinner_inset_event_beep);
        mBtnPlugInTestKey = (Button) rootView.findViewById(R.id.btn_plug_in_key_test);
        mBtnPlugInTestKey.setOnClickListener(this);
        mBtnPlugInTestCard = (Button) rootView.findViewById(R.id.btn_plug_in_card_test);
        mBtnPlugInTestCard.setOnClickListener(this);
        mBtnInsetTestKey = (Button) rootView.findViewById(R.id.btn_inset_key_test);
        mBtnInsetTestKey.setOnClickListener(this);
        mBtnInsetTestCard = (Button) rootView.findViewById(R.id.btn_inset_card_test);
        mBtnInsetTestCard.setOnClickListener(this);
    }

    @Override
    protected void initialized() {
        presenter = new ReaderPresenter(getContext(), this);
        mBeepArray = new ArrayAdapter<>(getContext(), R.layout.support_simple_spinner_dropdown_item,
                getResources().getStringArray(R.array.beep_type));
        mSpPlugInConnectBeep.setAdapter(mBeepArray);
        mSpPlugInUpdateBeep.setAdapter(mBeepArray);
        mSpPlugInEventBeep.setAdapter(mBeepArray);
        mSpInsetConnectBeep.setAdapter(mBeepArray);
        mSpInsetUpdateBeep.setAdapter(mBeepArray);
        mSpInsetEventBeep.setAdapter(mBeepArray);
        presenter.initData();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_plug_in_key_test:
                presenter.test(ReaderPresenter.PLUG_IN_READER, ReaderPresenter.KEY);
                break;
            case R.id.btn_plug_in_card_test:
                presenter.test(ReaderPresenter.PLUG_IN_READER, ReaderPresenter.CARD);
                break;
            case R.id.btn_inset_key_test:
                presenter.test(ReaderPresenter.INSET_READER, ReaderPresenter.KEY);
                break;
            case R.id.btn_inset_card_test:
                presenter.test(ReaderPresenter.INSET_READER, ReaderPresenter.CARD);
                break;
            default:
                break;
        }
    }

    @Override
    public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
        switch (parent.getId()) {
            case R.id.spinner_plug_in_connect_beep:
                presenter.beep(position, ReaderPresenter.PLUG_IN_CONNECT_BEEP, true);
                break;
            case R.id.spinner_plug_in_update_beep:
                presenter.beep(position, ReaderPresenter.PLUG_IN_UPDATE_BEEP, true);
                break;
            case R.id.spinner_plug_in_event_beep:
                presenter.beep(position, ReaderPresenter.PLUG_IN_EVENT_BEEP, true);
                break;
            case R.id.spinner_inset_connect_beep:
                presenter.beep(position, ReaderPresenter.INSET_CONNECT_BEEP, true);
                break;
            case R.id.spinner_inset_update_beep:
                presenter.beep(position, ReaderPresenter.INSET_UPDATE_BEEP, true);
                break;
            case R.id.spinner_inset_event_beep:
                presenter.beep(position, ReaderPresenter.INSET_EVENT_BEEP, true);
                break;
            default:
                break;
        }
    }

    @Override
    public void onNothingSelected(AdapterView<?> parent) {

    }



    @Override
    public void showProgressBar(boolean isShow) {

    }

    @Override
    public void showSnackBar(String txt) {
        if (null != txt) showShortSnackbar(rootView, txt, null);
    }

    @Override
    public void showAlertDialog(String txt) {
        if (null != txt)
            new AlertDialog.Builder(getContext()).setTitle(R.string.app_name).setMessage(txt)
                    .setIcon(R.mipmap.ic_launcher).setPositiveButton(R.string.okay, new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    dialog.dismiss();
                }
            }).show();
    }

    @Override
    public void setBeepType(ReaderData readerData) {
        if (null == mBeepArray || getResources().getStringArray(R.array.beep_type).length != mBeepArray.getCount())
            return;
        if (null != mSpPlugInConnectBeep)
            mSpPlugInConnectBeep.setSelection(readerData.getConnect_beep_1(), true);
        if (null != mSpPlugInUpdateBeep)
            mSpPlugInUpdateBeep.setSelection(readerData.getUpdate_beep_1(), true);
        if (null != mSpPlugInEventBeep)
            mSpPlugInEventBeep.setSelection(readerData.getEvent_beep_1(), true);
        if (null != mSpInsetConnectBeep)
            mSpInsetConnectBeep.setSelection(readerData.getConnect_beep_2(), true);
        if (null != mSpInsetUpdateBeep)
            mSpInsetUpdateBeep.setSelection(readerData.getUpdate_beep_2(), true);
        if (null != mSpInsetEventBeep)
            mSpInsetEventBeep.setSelection(readerData.getEvent_beep_2(), true);

        mSpPlugInConnectBeep.setOnItemSelectedListener(this);
        mSpPlugInUpdateBeep.setOnItemSelectedListener(this);
        mSpPlugInEventBeep.setOnItemSelectedListener(this);
        mSpInsetConnectBeep.setOnItemSelectedListener(this);
        mSpInsetUpdateBeep.setOnItemSelectedListener(this);
        mSpInsetEventBeep.setOnItemSelectedListener(this);
    }
}
